package fr.teuteuf.runstickmanrun.entity;

import fr.teuteuf.runstickmanrun.action.ITargetedAction;

public abstract class AbstractCharacter extends AbstractEntity {
	
	private final int DOUBLEJUMP_COST = 10;
	
	protected float maxHealth = 0;
	protected float health = 0;
	protected float healthRegen = 0;
	protected float maxEnergy = 0;
	protected float energy = 0;
	protected float energyRegen = 0;
	protected ITargetedAction jumpOnAction, jumpedOnAction;

	public float getHealth() {
		return health;
	}

	public void setHealth(float health) {
		this.health = health;
	}

	public float getEnergy() {
		return energy;
	}

	public void setEnergy(float energy) {
		this.energy = energy;
	}

	public void setJumpOnAction(ITargetedAction jumpOnAction) {
		this.jumpOnAction = jumpOnAction;
	}

	public void setJumpedOnAction(ITargetedAction jumpedOnAction) {
		this.jumpedOnAction = jumpedOnAction;
	}
	
	public void jumpOn(AbstractCharacter target) {
		jumpOnAction.execute(this, target);
	}
	
	public void jumpedOn(AbstractCharacter source) {
		jumpedOnAction.execute(source, this);
	}
	
	public void setHealthRegen(float healthRegen) {
		this.healthRegen = healthRegen;
	}
	
	public void setEnergyRegen(float energyRegen) {
		this.energyRegen = energyRegen;
	}
	
	public void update(float elapsedTime) {
		super.update(elapsedTime);
		
		if(! isDead()) {
			health += elapsedTime * healthRegen;
			energy += elapsedTime * energyRegen;
		} else {
			health = 0;
		}
		
		if(health > maxHealth) {
			health = maxHealth;
		}
		
		if(energy > maxEnergy) {
			energy = maxEnergy;
		}
	}
	
	@Override
	public void jump(boolean b) {
		if(b && isJumping() && ! isDoubleJumping() && ! isDead() && velocity.y < 0f) {
			energy -= DOUBLEJUMP_COST;
		}
		super.jump(b);
	}
}
